package cn.topevery.garden.wapi.service;

import cn.topevery.garden.wapi.config.GreProperties;
import hw.topevery.basis.dal.util.UserUtil;
import hw.topevery.basis.entity.model.LoginParam;
import hw.topevery.basis.runtime.TokenData;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.Utility;
import hw.topevery.framework.omi.*;
import hw.topevery.framework.util.LogUtil;
import hw.topevery.framework.web.HttpResultCode;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;


/**
 * @author bingxin.xu
 */
public class OmiRuntimeServiceImpl implements RuntimeService {
    public static final RuntimeService value = new OmiRuntimeServiceImpl();
    public UserUtil userUtil;
    public GreProperties greProperties;

    private OmiRuntimeServiceImpl() {

    }

    //    @Override
//    public boolean checkHeader(RsHeader val) {
//        return val != null && RsHeader.chkVal.uid.equals(val.uid) && RsHeader.chkVal.pwd.equals(val.pwd);
//    }

    @Override
    public String executeCore(RsPara para) throws Exception {
        String result = null;
        switch (para.actTypeVal) {
            default:
                break;
        }
        return result;
    }

    /**
     * 获取RSA
     *
     * @param para
     * @return
     */
    private String getRsa(RsPara para) throws IOException {
        RsResult result = new RsResult();
        InputStream inputStream = new ClassPathResource("rsa").getInputStream();
        String key = readRsa(inputStream);
        result.success = true;
        result.isZipDataJsonVal = false;
        result.code = 200;
        result.dataJsonVal = Utility.jsonSerialize(key);
        return Utility.jsonSerialize(result);
    }

    /**
     * 解析文件
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String readRsa(InputStream inputStream) throws IOException {
        String key;
        // 以文件流的形式逐行读取txt文件(文件存储编码需要utf-8格式，不然会乱码)
        InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(in);
        while ((key = br.readLine()) != null) {
            return key;
        }
        return null;
    }

    @Override
    public String executeCoreUpload(RsPara para, MultipartFile part) {
        return null;
    }

    @Override
    public RsDownloadResult executeCoreDownload(RsPara para) throws Exception {
        return null;
    }

    private String login(RsPara para) {
        RsResult result = new RsResult();

        return Utility.jsonSerialize(result);
    }

}

